/**
 * Board.java Copyright (c) Sportnerds.
 * <P></P>
 * POJO class for the TicTacToe game board.
 * @author Sportnerds
 * @version 1.0
 * @since 23.11.2011
 */
public class Board {

    protected char[] spaces= new char[9];

    /**
     * Constructor to initialize the game board array.
     * Each space is initialized with the number of its index+1 as char
     */
    public Board() {
        for(int i=0; i<9; i++)
        {
            this.spaces[i] = Integer.toString(i+1).charAt(0);
        }
    }

    /**
     * Returns the value of a space of the board
     * @param index which field is selected.
     * @return The number of the index+1 if the space is not occupied, else it returns X or O
     */
    public char getSpace(int index) {
        return spaces[index];
    }

    /**
     * To set value in specific field
     * @param index which field is selected.
     * @param symbol X for player 1 and O for player 2.
     */
    public void setSpace(int index, char symbol) {
        this.spaces[index] = symbol;
    }

    /**
     * Displays the game board in the console.
     */
    public void display()
    {
        for(int i=0; i<9; i++)
        {
            if((i)%3==0)System.out.print("\t");
            System.out.print("[" + spaces[i] + "] ");
            if((i+1)%3==0)System.out.print("\n");
        }
    }
}
